package com.deliinsns.sys;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import com.deliinsns.sys.util.SystemConstant;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class BasicController  {
	
	public  void out(HttpServletRequest request, HttpServletResponse response,Messenger msg ) {
		response.setCharacterEncoding(SystemConstant.SYSCHARACTER);//设置系统编码
		Properties ppt= getCodeDesc(request);
		try {
					msg.setMsg(ppt.getProperty(msg.getMsgCode()));
					PrintWriter writer= response.getWriter();
					writer.write(msg.toString());
					writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public  void out(HttpServletRequest request, HttpServletResponse response,JSONObject json ) {
		response.setCharacterEncoding(SystemConstant.SYSCHARACTER);//设置系统编码
		Properties ppt= getCodeDesc(request);
		try {
			
					PrintWriter writer= response.getWriter();
					writer.write(json.toString());
					writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public boolean checkParam(String ...params){
		 for(String p : params){
			 if(StringUtils.isEmpty(p))
				 return false;
		 }  
		 return true;
	}
	public Properties getCodeDesc(HttpServletRequest request) {
		ServletContext servletContext= request.getSession().getServletContext();
		return (Properties) servletContext.getAttribute("codeDesc");
	}
	public void  convert2BeanList(String dataStr,List<JSONObject> list) throws InstantiationException, IllegalAccessException, InvocationTargetException{
		JSONArray ja= JSONArray.fromObject(dataStr);
		JSONObject jo=null;
		Object obj=null;
		Iterator it= ja.iterator();
		while(it.hasNext()){
			jo= (JSONObject)it.next();
			list.add(jo);
		}
	}
	public Map<String,String> getParamMap(HttpServletRequest request){
		Map<String,String> map = new HashMap<String,String>();
		Enumeration enu=request.getParameterNames();
		String paraName=null;
		while(enu.hasMoreElements()){
			paraName=(String)enu.nextElement();
			map.put(paraName, request.getParameter(paraName));
		}
	    return map;
	}
}

